﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BLL;
using GUI.Common;

namespace GUI
{
    public partial class LichSuMuaHang : System.Web.UI.Page
    {
        private List<HoaDonDTO> lstHD;
        private HoaDonBLL hdBLL;
        private HoaDonDTO hdDTO;
        private KhachHangDTO khDTO;
        private KhachHangBLL khBLL;
        private List<GioHangDTO> lstGH;
        private string mahoadon;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Panel2.Visible = false;
                lstHD = new List<HoaDonDTO>();
                hdBLL = new HoaDonBLL();
                khBLL = new KhachHangBLL();


                khDTO = new KhachHangDTO();
                khDTO = (KhachHangDTO)Session["KhachHang"];

                if (khDTO.MaKhachHang != null)
                {
                    loadGridViewLichSuDatHang();
                    loadThongTinKhachHang(khDTO);
                }
            }
            catch
            {
                throw;
            }

        }

        #region ------------- xem danh sách lịch sử đặt hàng -----------------------


        public void loadGridViewLichSuDatHang()
        {
            lstHD = hdBLL.GetListHoaDonMotKhachHang(khDTO.MaKhachHang);
            GVLichSuDatHang.DataSource = lstHD;
            GVLichSuDatHang.DataBind();
            loadTinhTrang();

        }

        public void loadTinhTrang()
        {
            for (int i = 0; i < GVLichSuDatHang.Rows.Count; i++)
            {
                Label lbltinhtrang = (Label)GVLichSuDatHang.Rows[i].Cells[3].FindControl("lbltinhtrang");
                if (lbltinhtrang.Text.Equals("0"))
                    lbltinhtrang.Text = "Đang chờ xét duyệt";
                else
                    lbltinhtrang.Text = "Đã mua";
            }
        }

        #endregion


        #region ------------------- hiển thị chi tiết từng lần đặt hàng ---------------------

        public void loadThongTinKhachHang(KhachHangDTO khDTO)
        {
            lblHoTenKH.Text = khDTO.TenKhachHang;
            lblDiaChiKH.Text = khDTO.DiaChi;
            lblDienThoaiKH.Text = khDTO.DienThoai;
            lblEmailKH.Text = khDTO.Email;
        }

        public void loadThongTinNguoiNhan(NguoiNhanHangDTO nnhDTO)
        {
            lblHoTenNN.Text = nnhDTO.TenNguoiNhanHang;
            lblDiaChiNN.Text = nnhDTO.DiaChi;
            lblDienThoaiNN.Text = nnhDTO.DienThoai;
        }

        #endregion

        protected void GVLichSuDatHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GVLichSuDatHang.SelectedRow;
            Label lblhoadon = (Label)row.Cells[0].FindControl("lblhoadon");
            mahoadon = lblhoadon.Text;
            Session["mahoadon"] = mahoadon;
            hdDTO = new HoaDonDTO();
            hdDTO = hdBLL.GetHoaDon(mahoadon);

            Panel1.Visible = false;
            Panel2.Visible = true;

            NguoiNhanHangDTO nnhDTO = new NguoiNhanHangDTO();
            NguoiNhanHangBLL nnhBLL = new NguoiNhanHangBLL();
            nnhDTO = nnhBLL.GetNguoiNhanHang(hdDTO.MaNguoiNhanHang.ToString());

            loadThongTinNguoiNhan(nnhDTO);


            HinhThucGiaoHangDTO htghDTO = new HinhThucGiaoHangDTO();
            HinhThucGiaoHangBLL htghBLL = new HinhThucGiaoHangBLL();
            htghDTO = htghBLL.GetHinhThucGiaoHang(Convert.ToInt32(hdDTO.MaHinhThucGiaoHang));
            lblPTGH.Text = htghDTO.TenHinhThucGiaoHang;

            HinhThucThanhToanBLL htttBLL = new HinhThucThanhToanBLL();
            HinhThucThanhToanDTO htttDTO = new HinhThucThanhToanDTO();
            htttDTO = htttBLL.GetHinhThucThanhToan(Convert.ToInt32(hdDTO.MaHinhThucThanhToan));
            lblPTTT.Text = htttDTO.TenHinhThucThanhToan;

            //load thong tin gio hang
            lstGH = hdBLL.GetHoaDonMotKhachHangVaoGioHang(hdDTO.MaHoaDon);

            GVGioHang.DataSource = lstGH;
            GVGioHang.DataBind();
            String tongtien = String.Format("{0:0,0}", hdDTO.TongTien);
            ((Label)GVGioHang.FooterRow.Cells[4].FindControl("lbltongtien")).Text = tongtien;

            if (hdDTO.TinhTrangHoaDon == 0)
                btnHuyDatHang.Visible = true;
            else
                btnHuyDatHang.Visible = false;
        }

        protected void btnQuayVe_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }

        #region protected void btnHuyDatHang_Click(object sender, EventArgs e)
        protected void btnHuyDatHang_Click(object sender, EventArgs e)
        {
            hdBLL.huyHoaDon(Session["mahoadon"].ToString());
            Panel1.Visible = true;
            Panel2.Visible = false;
            Page_Load(sender, e);
            Label1.Visible = true;
        }
        #endregion

        #region protected void GVLichSuDatHang_RowDataBound(object sender, GridViewRowEventArgs e)
        protected void GVLichSuDatHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#bde2fd'");

                if (e.Row.RowIndex % 2 == 0)
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='#EFF3FB'");
                else
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='White'");

            }
        }
        #endregion

        #region protected void GVGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        protected void GVGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#bde2fd'");

                if (e.Row.RowIndex % 2 == 0)
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='#EFF3FB'");
                else
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='White'");

            }
        }
        #endregion


        #region LoadFullDateTime
        protected string LoadFullDateTime(object date)
        {
            return CommonClass.FormatFullDateTime((DateTime)date);
        }
        #endregion
    }
}